package com.buddy.summary.code.algorithm;

import com.google.common.math.DoubleMath;

public class Queue {
    private int[] array;
    private int front;//队头
    private int rear;//队尾

    public Queue(int capacity) {
        this.array = new int[capacity];
    }

    private void enqueue(int element) throws Exception {
        if ((rear + 1) % array.length == front) {
            throw new Exception("队列已满!");
        }
        array[rear] = element;
        rear = (rear+1)%array.length;
    }

    private int dequeue() throws Exception {
        if (rear == front){
            throw new Exception("队列已空!");
        }
        int dequeueElement = array[front];
        front = (  +1)%array.length;
        return dequeueElement;
    }

    private void output(){
        for (int i=front;i!=rear;i=(i+1)%array.length){
            System.out.print(array[i]+" ");
        }
        System.out.println("");
    }

    public static void main(String[] args) throws Exception {
        Queue queue = new Queue(6);
        for (int i=0;i<5;i++){
            queue.enqueue((int) (Math.random()*10));
            queue.output();
        }
        System.out.println(queue.dequeue());
    }

}
